package com.bootdo.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.config.Constant;
import com.bootdo.common.controller.BaseController;
import com.bootdo.system.domain.DeptDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.system.domain.WeekDO;
import com.bootdo.system.service.WeekService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author huizhang
 * @email 
 * @date 2021-03-04 10:22:38
 */
 
@Controller
@RequestMapping("/system/week")
public class WeekController extends BaseController {
	@Autowired
	private WeekService weekService;
	
	@GetMapping()
	@RequiresPermissions("system:week:week")
	String Week(){
	    return "system/week/week";
	}
	
	@ResponseBody
	@GetMapping("/list")
	/*@RequiresPermissions("system:week:week")*/
	public List<WeekDO> list(@RequestParam Map<String, Object> params){
		List<WeekDO> list = weekService.list(params);
		return list;
	}

    @ResponseBody
    @GetMapping("/listFor")
    public List<WeekDO> listFor(WeekDO weekDO){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("parentId",weekDO.getParentId());
        List<WeekDO> list = weekService.list(params);
        return list;
    }

	
	@GetMapping("/add/{pId}")
	/*@RequiresPermissions("system:week:add")*/
	String add(@PathVariable("pId") Long pId, Model model){
		model.addAttribute("pId", pId);
		if (pId == 0) {
			model.addAttribute("pName", "总学期");
		} else {
			model.addAttribute("pName", weekService.get(pId).getWeekName());
		}
	    return "system/week/add";
	}

	@GetMapping("/edit/{weekId}")
	/*@RequiresPermissions("system:week:edit")*/
	String edit(@PathVariable("weekId") Long weekId,Model model){
		WeekDO week = weekService.get(weekId);
		model.addAttribute("week", week);
		if(Constant.DEPT_ROOT_ID.equals(week.getParentId())) {
			model.addAttribute("parentDeptName", "总学期");
		}else {
			WeekDO parWeek = weekService.get(week.getParentId());
			model.addAttribute("parentDeptName", parWeek.getWeekName());
		}
	    return "system/week/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	/*@RequiresPermissions("system:week:add")*/
	public R save( WeekDO week){
		if(weekService.save(week)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	/*@RequiresPermissions("system:week:edit")*/
	public R update( WeekDO week){
		weekService.update(week);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	/*@RequiresPermissions("system:week:remove")*/
	public R remove( Long weekId){
		if(weekService.remove(weekId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	/*@RequiresPermissions("system:week:batchRemove")*/
	public R remove(@RequestParam("ids[]") Long[] weekIds){
		weekService.batchRemove(weekIds);
		return R.ok();
	}
	
}
